package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.BatchSurplusRecordDao;
import com.xbongbong.saas.domain.entity.BatchSurplusRecordEntity;
import com.xbongbong.saas.model.BatchSurplusRecordModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service("batchSurplusRecordModel")
public class BatchSurplusRecordModelImpl implements BatchSurplusRecordModel {
    @Resource
    private BatchSurplusRecordDao batchSurplusRecordDao;
    @Override
    public Integer insert(BatchSurplusRecordEntity stockSurplusRecordEntity) {
        stockSurplusRecordEntity.setAddTime(DateTimeUtil.getInt());
        stockSurplusRecordEntity.setUpdateTime(DateTimeUtil.getInt());
        return batchSurplusRecordDao.insert(stockSurplusRecordEntity);
    }

    @Override
    public Integer update(BatchSurplusRecordEntity stockSurplusRecordEntity)  {
        stockSurplusRecordEntity.setUpdateTime(DateTimeUtil.getInt());
        return batchSurplusRecordDao.update(stockSurplusRecordEntity);
    }

    @Override
    public List<BatchSurplusRecordEntity> findEntitys(Map<String, Object> param) {
        return batchSurplusRecordDao.findEntitys(param);
    }

    @Override
    public Integer save(BatchSurplusRecordEntity stockSurplusRecordEntity) {
        if (stockSurplusRecordEntity.getId() == null || stockSurplusRecordEntity.getId().equals(0L)) {
            return insert(stockSurplusRecordEntity);
        }
        return update(stockSurplusRecordEntity);
    }

    @Override
    public void insertBatch(List<BatchSurplusRecordEntity> stockSurplusRecordEntityList) {
        batchSurplusRecordDao.insertBatch(stockSurplusRecordEntityList);
    }

    @Override
    public List<BatchSurplusRecordEntity> getLatestFlowBillByBatchKey(String corpid, Set<String> batchKeySet, Long dateTime) {
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put(ParameterConstant.CORPID, corpid);
        params.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        params.put("lessEqualThanDate", dateTime);
        params.put("orderByStr", " date desc, id desc");
        params.put("batchKeyIn", batchKeySet);
        return batchSurplusRecordDao.getLatestFlowBillByBatchKey(params);
    }

    @Override
    public void updateBatch(List<BatchSurplusRecordEntity> list, String corpid) {
        batchSurplusRecordDao.updateBatch(list, corpid);
    }

    @Override
    public void deleteAllByCorpId(String corpid) {
        batchSurplusRecordDao.deleteAllByCorpId(corpid);
    }

    @Override
    public void insertRecordsFromBatchStock(String corpid, Long monthBeforeInt) {
        batchSurplusRecordDao.insertRecordsFromBatchStock(corpid, monthBeforeInt);
    }

    @Override
    public void deleteByAddTime(Map<String, Object> param) {
        batchSurplusRecordDao.deleteByAddTime(param);
    }

    @Override
    public void batchInsertRecordsFromBatchStock(Map<String, Object> param) {
        batchSurplusRecordDao.batchInsertRecordsFromBatchStock(param);
    }
}
